package com.woniuxy.commentweb.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.commentmodel.dto.CommentDto;
import com.woniuxy.commentmodel.form.*;
import com.woniuxy.commentmodel.param.*;
import com.woniuxy.commentservice.service.CommentService;
import com.woniuxy.core.exception.ParameterValidException;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.core.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 * 场馆评价表 前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/comment")
@Api(tags = "评论相关接口")
public class CommentController {
    @Resource
    private CommentService commentService;

    /**
     *
     * @param form 新增评论参数列表（场馆id，评论内容，评论图片（可不传），订单编号，评分）
     * @param token 登录的用户token
     * @return 无
     */

    @ApiOperation("新增评论方法")
    @PostMapping("/add")
    public Result<?> add(@RequestBody @Validated AddCommentForm form,@RequestHeader String token){
        Integer userId = MyJwtUtil.getUserIdByKey(token, "userId");
        AddCommentParam param = BeanUtil.toBean(form, AddCommentParam.class);
        param.setUserId(userId);
        commentService.add(param);
        return Result.success("新增评论成功");
    }

    /**
     *
     * @param form 回复评论参数列表（回复内容，所回复评论id）
     * @param token 登录的场馆token
     * @return 无
     */
    @ApiOperation("场馆回复评论方法")
    @PostMapping("/reply.venue")
    public Result<?> reply(@RequestBody @Validated ReplyForm form,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        ReplyParam param = BeanUtil.toBean(form, ReplyParam.class);
        param.setVenueId(venueId);
        commentService.reply(param);
        return Result.success("回复评论成功");
    }

    /**
     *
     * @param form 用户分页查看评论参数列表（页数，条数，场馆id）
     * @return pageInfo对象
     */
    @ApiOperation("用户分页查看评论方法")
    @PostMapping("/userByPage")
    public Result<PageInfo<CommentDto>> UserByPage(@RequestBody @Validated CommentByUserForm form){
        CommentByPageParam param = BeanUtil.toBean(form, CommentByPageParam.class);
        PageInfo<CommentDto> pageInfo = commentService.byPage(param);
        return Result.success("用户分页查看评论成功",pageInfo);
    }

    /**
     *
     * @param form 用户分页查看评论参数列表（页数，条数）
     * @param br 参数校验对象
     * @param token 场馆token
     * @return pageInfo对象
     */
    @ApiOperation("场馆分页查看评论方法")
    @PostMapping("/venueByPage.venue")
    public Result<PageInfo<CommentDto>> VenueByPage(@RequestBody @Validated CommentByVenueForm form, BindingResult br, @RequestHeader String token){
        if (br.hasErrors()) throw new ParameterValidException(500,"参数输入有误");
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        CommentByPageParam param = BeanUtil.toBean(form, CommentByPageParam.class);
        param.setVenueId(venueId);
        PageInfo<CommentDto> pageInfo = commentService.byPage(param);
        return Result.success("场馆分页查看评论成功",pageInfo);
    }

    /**
     *
     * @param commentId 评论id
     * @param token 场馆token
     * @return 无
     */
    @ApiOperation("场馆删除评论方法")
    @GetMapping("/venueDel.venue")
    public Result<?> venueDel(Integer commentId,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        commentService.venueDel(commentId,venueId);
        return Result.success("场馆删除评论成功");
    }

    /**
     *
     * @param commentId 评论id
     * @param token 用户token
     * @return 无
     */
    @ApiOperation("用户删除评论方法")
    @GetMapping("/userDel")
    public Result<?> userDel(Integer commentId,@RequestHeader String token){
        Integer userId = MyJwtUtil.getUserIdByKey(token, "userId");
        commentService.userDel(commentId,userId);
        return Result.success("用户删除评论成功");
    }
    /**
     *
     * @param commentId 评论id
     * @param token 场馆token
     * @return 无
     */
    @ApiOperation("场馆删除回复方法")
    @GetMapping("/replyDel.venue")
    public Result<?> replyDel(Integer commentId,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        commentService.replyDel(commentId,venueId);
        return Result.success("场馆删除回复成功");
    }

    /**
     *
     * @param form （场馆id，评论内容，评论图片（可不传），订单编号，评分）
     * @param token 用户token
     * @return 无
     */
    @ApiOperation("用户修改评论方法")
    @PostMapping("/userModify")
    public Result<?> userModify(@RequestBody @Validated UserModifyForm form,@RequestHeader String token){
        Integer userId = MyJwtUtil.getUserIdByKey(token, "userId");
        UserModifyParam param = BeanUtil.toBean(form, UserModifyParam.class);
        param.setUserId(userId);
        commentService.userModify(param);
        return Result.success("用户修改评论成功");
    }

    /**
     *
     * @param form 场馆修改回复参数列表（回复内容，评论id）
     * @param token 场馆token
     * @return 无
     */
    @ApiOperation("场馆修改回复方法")
    @PostMapping("/venueModify.venue")
    public Result<?> venueModify(@RequestBody @Validated VenueModifyForm form,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        VenueModifyParam param = BeanUtil.toBean(form, VenueModifyParam.class);
        param.setVenueId(venueId);
        commentService.venueModify(param);
        return Result.success("场馆修改回复成功");
    }

    @PostMapping("/upload")
    @ApiOperation("上传评论图片方法")
    public Result<String> upload(MultipartFile file, String orderNum) throws IOException {
        String url = commentService.upload(file,orderNum);
        return Result.success("图片上传成功",url);
    }
}

